home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-03-15 | 6.0 KB | 176 lines | [TEXT/gamI] |
- (##include "header.scm alias")
- ; Read eval print loop
- ;;;the #$#@) compiler doesn't like default args
- ;(define (thomas-rep (in ##stdin) (out ##stdout) (prompt2 "? ") (prompt1 ""))
- ; (##call-with-current-continuation
- ; (lambda (cont) (thomas-read-eval-print in out prompt2 prompt1 cont))))
-
- (define (thomas-rep in out prompt2 prompt1)
- (##call-with-current-continuation
- (lambda (cont) (thomas-read-eval-print in out prompt2 prompt1 cont))))
-
- ;(define ##repl-write #f)
- ;(set! ##repl-write #f)
-
- ;(define ##repl-read #f)
- ;(set! ##repl-read #f)
-
- (define (thomas-read-eval-print in out prompt2 prompt1 cont)
-
- (define (repl-start subprobs repl-info dyn-bindings)
-
- (define (repl-read)
- (let ((proc ##repl-read))
- (if (##procedure? proc)
- (proc in)
- (##read in))))
-
- (define (repl-write val)
- (let ((proc ##repl-write))
- (if (##procedure? proc)
- (proc val out)
- (begin
- (display val out)
- (##newline out)))))
-
- (define (repl-n n)
- (let loop ((i 0) (s subprobs))
- (if (and (##fixnum.< n i) (##pair? (##cdr s)))
- (loop (##fixnum.- i 1) (##cdr s))
- (let ((f (##car s)))
- (##display-subproblem i f out)
- (repl i s f)))))
-
- (define (cmd-d)
- (let ((l (##cdr (##vector-ref repl-info 3))))
- (if (##pair? l)
- ((##car l) #f)
- (begin
- (##newline out)
- (##write-string "*** ^D again to exit" out)
- (##newline out)
- (if (##eof-object? (##peek-char in))
- (##quit))))))
-
- (define (cmd-t)
- (let loop ((l (##vector-ref repl-info 3)))
- (if (##pair? (##cdr l))
- (loop (##cdr l))
- ((##car l) #f))))
-
- (define (repl pos subprobs* f)
-
- (##call-with-current-continuation
- (lambda (abort)
- (##set-car! (##vector-ref repl-info 3) abort)))
-
- (let loop ()
-
- (##newline out)
- (##display prompt1 out #f)
- (if (##fixnum.< pos 0) (##display pos out #f))
- (##display prompt2 out #f)
-
- (let ((expr (repl-read)))
- (if (##eof-object? expr)
- (begin (cmd-d) (loop))
- (if (and (##pair? expr)
- (##pair? (##cdr expr))
- (##null? (##cddr expr))
- (##eq? (##car expr) 'UNQUOTE))
- (let ((cmd (##cadr expr)))
- (if (##eof-object? cmd)
- (begin (cmd-d) (loop))
- (case cmd
- ((?) (##cmd-? out) (loop))
- ((-) (repl-n (##fixnum.- pos 1)))
- ((+) (repl-n (##fixnum.+ pos 1)))
- ((b) (##cmd-b pos subprobs* out) (loop))
- ((i) (##cmd-i f out) (loop))
- ((y) (##cmd-y f out) (loop))
- ((l) (##cmd-l f out) (loop))
- ((t) (cmd-t))
- ((d) (cmd-d) (loop))
- ((r) (##display "Return value: " out #f)
- (let ((expr (repl-read)))
- (if (##eof-object? expr)
- ##undef-object
- (that-special-thomas-thing expr f dyn-bindings))))
- ((q) (##quit))
- (else
- (if (and (##fixnum? cmd) (##fixnum.< cmd 1))
- (repl-n cmd)
- (begin
- (##write-string "Unknown command ," out)
- (##write cmd out #f)
- (##newline out)
- (loop)))))))
- (cond ((eq? expr 'thomas:done) 'thomas:done)
- (#t
- (let ((val (that-special-thomas-thing expr f dyn-bindings)))
- ; (repl-write val)
- (loop)))))))))
-
- (repl 0 subprobs (##car subprobs)))
-
- (let ((repl-info (##make-vector 4 #f)))
- (let ((prev-info (##dynamic-ref '##REPL-INFO #f))
- (dyn-bindings (##list (##cons '##REPL-INFO repl-info))))
- (##vector-set! repl-info 0 in)
- (##vector-set! repl-info 1 out)
- (##vector-set! repl-info 2
- (if prev-info
- (##fixnum.+ (##vector-ref prev-info 2) 1)
- 0))
- (##vector-set! repl-info 3
- (##cons (lambda (x) (##quit))
- (if prev-info
- (##vector-ref prev-info 3)
- '())))
- (##dynamic-bind
- dyn-bindings
- (lambda ()
- (repl-start (##continuation->subproblems cont)
- repl-info
- dyn-bindings))))))
-
- (define (##debug-repl cont)
- (let ((repl-info (##dynamic-ref '##REPL-INFO #f)))
- (if repl-info
- (thomas-read-eval-print (##vector-ref repl-info 0)
- (##vector-ref repl-info 1)
- "? "
- (##fixnum.+ (##vector-ref repl-info 2) 1)
- cont)
- (thomas-read-eval-print ##stdin ##stdout ": " 0 cont))))
-
- (define (implementation-specific:eval expr f dyn-bindings)
- (##eval-within expr f dyn-bindings))
-
- (define (that-special-thomas-thing input f dyn-bindings)
- (compile-expression
- input '!MULTIPLE-VALUES thomas-rep-module-variables
- (lambda (new-vars preamble compiled-output)
- (implementation-specific:eval
- `(BEGIN
- ,@preamble
- (LET* ((!MULTIPLE-VALUES (VECTOR '()))
- (!RESULT ,compiled-output))
- (IF (EQ? !RESULT !MULTIPLE-VALUES)
- (LET RESULT-LOOP
- ((COUNT 1)
- (RESULTS (VECTOR-REF !MULTIPLE-VALUES 0)))
- (IF (PAIR? RESULTS)
- (LET ((RESULT (CAR RESULTS)))
- (NEWLINE)
- (DISPLAY ";Value[")(DISPLAY COUNT)
- (DISPLAY "]: ")(WRITE RESULT)
- (RESULT-LOOP (+ 1 COUNT) (CDR RESULTS)))
- (NEWLINE)))
- (BEGIN
- (NEWLINE)
- (DISPLAY ";Value: ")(WRITE !RESULT) (NEWLINE)))))
- f dyn-bindings)
- (set! thomas-rep-module-variables
- (append new-vars thomas-rep-module-variables))
- ))) ; uh oh - no loop?